package com.zj.leetcode.easy.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: algorithm
 * @description: N 叉树的前序遍历
 * 589 n-ary-tree-preorder-traversal
 * @author: Zhang Bo
 * @create: 2022-06-28 19:46:08
 **/
public class NAryTreePreorderTraversal {
    public static void main(String[] args) {
        Solution solution = new NAryTreePreorderTraversal().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    ;


    class Solution {
        List<Integer> list = new ArrayList<>();

        public List<Integer> preorder(Node root) {
            if (root == null) {
                return list;
            }
            this.dfs(root);
            return list;
        }

        private void dfs(Node node) {
            list.add(node.val);
            for (int i = 0; i < node.children.size(); i++) {
                this.dfs(node.children.get(i));
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
